package sample;

import org.dreamwork.config.ConfigEntry;
import org.dreamwork.config.ConfigParseException;
import org.dreamwork.config.IXMLConfigParser;
import org.dreamwork.config.ListEntry;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

/**
 * Created with IntelliJ IDEA.
 * User: seth
 * Date: 13-1-11
 * Time: 下午6:08
 */
public class HostParser implements IXMLConfigParser {
    @Override
    public Object parse (Element section) throws ConfigParseException {
        ListEntry listEntry = new ListEntry ();
        NodeList list = section.getElementsByTagName ("host");
        for (int i = 0; i < list.getLength (); i ++) {
            Element e = (Element) list.item (i);
            ConfigEntry entry = new ConfigEntry ();
            NamedNodeMap map = e.getAttributes ();
            for (int j = 0; j < map.getLength (); j ++) {
                Attr a = (Attr) map.item (j);
                entry.addValue (a.getName (), a.getValue ());
            }
            listEntry.addValue (null, entry);
        }
        return listEntry;
    }
}